#define ledR (12)
#define ledG (11)
#define sw (3)

#define SW_ON  (0)
#define SW_OFF  (1)
#define LED_ON  (0)
#define LED_OFF  (1)
#define initIval (1000) 

unsigned long previousMillis = 0, currentMillis = 0, previousBlinkMillis = 0;     

int ledState = 0;    //the ledState would be negative in some status 
unsigned char swState;
unsigned char swPrevState = 1;
unsigned char ledBlinkState = 0;

const int numberOfCase = 5;

// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
  pinMode(sw, INPUT);
  
  //make the two LED on
  digitalWrite(ledR, LED_ON);
  digitalWrite(ledG, LED_ON);
  
  delay(500);
}

void loop(){
  swState = digitalRead(sw); 
  if(swState == SW_OFF && swPrevState == SW_ON){
    ledState = (ledState + 1) % numberOfCase;
    previousMillis = millis();
  }
  else if(swState == SW_ON && swPrevState == SW_ON){
    //if you press the switch longer than 1s
    currentMillis = millis();
    if(currentMillis - previousMillis >= initIval){
      ledState = -1;
      previousMillis = millis();
    }
  }
  else
    previousMillis = millis();
  
  if(ledState == 0){
    digitalWrite(ledR, LED_ON);
    digitalWrite(ledG, LED_ON);
  }
  else if(ledState == 1){
    digitalWrite(ledG, LED_ON);
    digitalWrite(ledR,LED_OFF);
  }
  else if(ledState == 2){
    digitalWrite(ledR,LED_ON);
    digitalWrite(ledG,LED_OFF);
  }
  else if(ledState == 3){
    blink();
  }
  else if(ledState == 4){ 
    blinkWithDelay();
  }
  swPrevState = swState;
 }
 
 //the two LED on one by one 
 void blink(){
  digitalWrite(ledG, LED_ON);   
  digitalWrite(ledR, LED_OFF);
  delay(500);     
  
  digitalWrite(ledG, LED_ON);    
  digitalWrite(ledR, LED_OFF);
  delay(500);            
 }  

//the two LED on and off together and the time will delay 1s
 void blinkWithDelay()
{
  currentMillis = millis();
  if(currentMillis - previousBlinkMillis >= 1000){
    if(ledBlinkState == LED_ON)
      ledBlinkState = LED_OFF;
    else
      ledBlinkState = LED_ON;
    previousBlinkMillis = millis();
  } 
  
  digitalWrite(ledG, ledBlinkState);
  digitalWrite(ledR, ledBlinkState);
}


